﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebClass;
using System.Data;

namespace QuerySelectionLabelingTool
{
    public partial class PreAndRe : System.Web.UI.Page
    {
        ExperimentsQueries exp = new ExperimentsQueries();
        string strTaskID;
        protected void Page_Load(object sender, EventArgs e)
        {
            strTaskID = Request.QueryString["taskID"];
            Session["currentTaskID"] = strTaskID;
            labelNewsCompCount.Text = "Total News: " + exp.NewsTotalCount(Convert.ToInt32(strTaskID)).ToString();
            labelTaskName.Text = "Task: " + exp.GetTaskName(Convert.ToInt32(strTaskID));
        }

        public DataSet GetPreAndReDataSet()
        {
            DataTable dt = new DataTable();
            List<PrecisionRecallCoverage> listPrecisionRecallCoverage = exp.GetDataAndCalcuPrecisionRecall(Convert.ToInt32(Session["currentTaskID"]));
            
            dt.Columns.Add("name");
            dt.Columns.Add("Precision",System.Type.GetType("System.Double"));
            dt.Columns.Add("PrecisionData",System.Type.GetType("System.String"));
            dt.Columns.Add("Recall",System.Type.GetType("System.Double"));
            dt.Columns.Add("RecallData", System.Type.GetType("System.String"));
            dt.Columns.Add("Coverage", System.Type.GetType("System.Double"));
            dt.Columns.Add("CoverageData", System.Type.GetType("System.String"));
            foreach (PrecisionRecallCoverage preAndReCover in listPrecisionRecallCoverage)
            {
                DataRow row = dt.NewRow();

                row[0] = preAndReCover.ExperimentName;
                row[1] = preAndReCover.PrecisionOutput.Result;
                row[2] = "(" + preAndReCover.PrecisionInput.TP + "/" + (preAndReCover.PrecisionInput.TP + preAndReCover.PrecisionInput.FP) + ")";
                row[3] = preAndReCover.RecallOutput.Result;
                row[4] = "(" + preAndReCover.RecallInput.TP + "/" + (preAndReCover.RecallInput.TP + preAndReCover.RecallInput.FN) + ")";
                row[5] = preAndReCover.CoverageOutput.Result;
                row[6] = "(" + preAndReCover.CoverageInput.HitTargetCount + "/" + preAndReCover.CoverageInput.Total + ")";
                dt.Rows.Add(row);
            }
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            ViewState["dataSet"] = ds;
            return ds;
        }
        protected void GridView2_DataBound(object sender, EventArgs e)
        {
            foreach (TableRow r in this.GridView2.Rows)
            {
                //set "Incomplete" is "0"
                if (r.Cells[1].Text.ToString() == "&nbsp;")
                {
                    r.Cells[1].Text = "0";
                }
            }
        }

        protected void Chart1_DataBinding(object sender, EventArgs e)
        {
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            int row = GridView1.Rows.Count - 1;
            if (row < 1)
            {
                row = 1;
            }
            this.Chart1.Height = row * 80;
            //merge "Precision" and "PrecisionData", it is the same as "Recall" and "Coverage".
            DataTable dt = GetPreAndReDataSet().Tables[0];
            for (int i = 0; i < this.GridView1.Rows.Count; ++i)
            {
                GridViewRow r = this.GridView1.Rows[i];
                string precisionData = dt.Rows[i]["PrecisionData"].ToString();
                string recallData = dt.Rows[i]["RecallData"].ToString();
                string coverageData = dt.Rows[i]["CoverageData"].ToString();

                r.Cells[1].Text = r.Cells[1].Text + " " + precisionData;
                r.Cells[2].Text = r.Cells[2].Text + " " + recallData;
                r.Cells[3].Text = r.Cells[3].Text + " " + coverageData;
            }
        }
    }
}